package com.myjoje.config;

import com.myjoje.base.BaseUtil;
import com.myjoje.bean.system.MenuBean;
import com.myjoje.bean.system.MenuRoleBean;
import com.myjoje.bean.system.RoleBean;
import com.myjoje.bean.system.UserBean;
import com.myjoje.dao.system.MenuDao;
import com.myjoje.jpa.MenuJpa;
import com.myjoje.jpa.MenuRoleJpa;
import com.myjoje.jpa.RoleJpa;
import com.myjoje.jpa.UserJpa;
import com.myjoje.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 程序初始化
 */
@Component
@Async
public class AppInitData {

    @Autowired
    private UserService userService;

    @Autowired
    private UserJpa userJpa;

    @Autowired
    private RoleJpa roleJpa;

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private MenuJpa menuJpa;

    @Autowired
    private MenuRoleJpa menuRoleJpa;

    //初始化数据
    @PostConstruct
    public void initData() {
        //初始化admin用户
        UserBean user = initUserAndRole("myjoje");

        //初始化菜单
        Map map = new HashMap();
        map.put("name", "系统管理");
        map.put("parentId", null);
        List list = menuDao.get(null, null, map);
        if (list.size() == 0) {//没有系统管理菜单，初始化
            initMenu(user);
        }

        //缓存所有角色
        initRoleCache();
    }

    /**
     * 初始化菜单
     */
    private void initMenu(UserBean user) {
        String userId = user.getId();
        String parentId = getMenuBean("系统管理", "/system", null, "10", userId);
        String workbench = getMenuBean("工作台", "/admin/workbench/workbench_index", null, "1", userId);
        String menu1 = getMenuBean("用户管理", "/system/user/user_index", parentId, "1", userId);
        String menu2 = getMenuBean("菜单管理", "/system/menu/menu_index", parentId, "2", userId);
        String menu3 = getMenuBean("角色管理", "/system/user/role_index", parentId, "3", userId);
        String menu4 = getMenuBean("部门管理", "/system/dept/dept_index", parentId, "4", userId);
        String roleId = user.getRoleIds();
        MenuRoleBean bean = menuRoleJpa.findByRoleId(roleId);
        if (bean == null) {
            bean = new MenuRoleBean();
            bean.setRoleId(roleId);
        }
        //系统管理员默认加上此菜单
        bean.setMenuIds(workbench + "," + parentId + "," + menu1 + "," + menu2 + "," + menu3 + "," + menu4);
        bean.setCreateUser(userId);
        bean.setCreateTime(BaseUtil.getDateTime());
        menuRoleJpa.save(bean);
    }

    /**
     * 获取菜单，没有创建
     */
    private String getMenuBean(String name, String url, String parentId, String priority, String userId) {
        MenuBean bean = new MenuBean();
        bean.setLabel(name);
        bean.setPath(url);
        bean.setPriority(priority);
        bean.setCreateUser(userId);
        bean.setParentId(parentId);
        bean.setCreateTime(BaseUtil.getDateTime());
        bean.setStatus("1");
        menuJpa.save(bean);
        return bean.getId();
    }

    /**
     * 缓存所有角色
     */
    private void initRoleCache() {
        List<RoleBean> list = roleJpa.findAll();
        list.stream().forEach(bean -> AppCache.setRole(bean.getCode(), bean.getId()));
    }

    /**
     * 获取角色，没有创建
     */
    private RoleBean getRoleBean(String code, String name, String priority, String userId) {
        RoleBean bean = roleJpa.findByCode(code);
        //添加系统管理员角色
        if (null == bean) {
            bean = new RoleBean();
            bean.setName(name);
            bean.setCode(code);
            bean.setPriority(priority);
            bean.setCreateUser(userId);
            bean.setCreateTime(BaseUtil.getDateTime());
            roleJpa.save(bean);
        }
        return bean;
    }

    /**
     * 初始化用户和角色
     *
     * @return
     */
    private UserBean initUserAndRole(String username) {
        UserBean userBean = userJpa.findByUsername(username);
        if (null == userBean) {
            userBean = userBean = getUserBean(username, "超级管理员", "13525576123",
                    "myjoje111", null, null);
            String userId = userBean.getId();
            RoleBean rootRole = getRoleBean("root", "超级管理员", "1", userId);
            userBean.setCreateUser(userId);
            userBean.setRoleIds(rootRole.getId());
            userJpa.save(userBean);
            //创建系统管理员角色
            RoleBean adminRole = getRoleBean("admin", "系统管理员", "2", userId);
            //创建管理员测试账号
            getUserBean("11001", "系统管理员", null, null, userId, adminRole.getId());

        }
        return userBean;
    }

    private UserBean getUserBean(String username, String nickname, String phoneNum, String openId,
                                 String createUser, String roleId) {
        UserBean userBean = new UserBean();
        userBean.setUsername(username);
        userBean.setNickname(nickname);
        userBean.setPassword(userService.getDefaultPassword());
        userBean.setProvince("河南省");
        userBean.setCity("郑州市");
        userBean.setPhoneNum(phoneNum);
        userBean.setOpenid(openId);
        userBean.setStatus(UserBean.STATUS_NORMAL);
        userBean.setCreateTime(BaseUtil.getDateTime());
        userBean.setCreateUser(createUser);
        userBean.setRoleIds(roleId);
        userJpa.save(userBean);
        return userBean;
    }

}
